//
//  NewProductTableViewCell.swift
//  Nahuo
//
//  Created by zang qilong on 2017/5/12.
//  Copyright © 2017年 zang qilong. All rights reserved.
//

import UIKit
import YYWebImage

class NewProductTableViewCell: UITableViewCell {

    @IBOutlet weak var productNameLabel: UILabel!
    @IBOutlet weak var originalPriceLabel: UILabel!
    @IBOutlet weak var discountLabel: UILabel!
    @IBOutlet weak var productCollectionView: UICollectionView!
    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var favouriteButton: UIButton!
    @IBOutlet weak var resellButton: UIButton!
    
    var imgs:[String] = [String]()
    var videos:[String] = [String]()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.contentView.backgroundColor = UIColor.tableBGColor()
        productCollectionView.register(UINib.init(nibName: ProductNewCollectionViewCell.reuseID(), bundle: nil), forCellWithReuseIdentifier: ProductNewCollectionViewCell.reuseID())
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func setupCellWithProductModel(model:Product) {
        productNameLabel.text = model.name
        originalPriceLabel.text = "¥\(model.retailPrice!)"
        discountLabel.text = "¥\(model.tradePrice!)"
        dateLabel.text = model.createTime
        
        if let imgsURLArray = model.imgs {
            imgs = imgsURLArray
        }
        
        if let videosURLArray = model.videos {
            videos = videosURLArray
        }
        
        self.productCollectionView.reloadData()
    }
    
}

extension NewProductTableViewCell:UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ProductNewCollectionViewCell.reuseID(), for: indexPath) as! ProductNewCollectionViewCell
        cell.productImageView.yy_setImage(with: URL(string: imgs[indexPath.row]), options: .progressiveBlur)
        if indexPath.row < videos.count {
            cell.playButton.isHidden = false
            cell.videoURL = videos[indexPath.row]
        }else {
            cell.playButton.isHidden = true
            cell.videoURL = nil
        }
        return cell;
        
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imgs.count
    }
}

extension NewProductTableViewCell:UICollectionViewDelegate {
    
}
